package OOday04;


public class PolymorphicDemo {
    public static void main(String[] args) {
        //演示向上造型
        /*Dog o1 = new Dog("小黑",1,"黑");
        Animal o2 = new Dog("小黑",2,"黑");
        Swim o3 = new Dog("小黑",3,"黑");*/

        //演示向上造型(多态)的第一点应用
        /*Master master = new Master();
        Dog dog = new Dog("小黑",2,"黑");
        Chick chick = new Chick("小花",1,"花");
        Fish fish = new Fish("小金",2,"金");
        master.feed(dog);//在传参的同时,系统自动做了向上造型
        master.feed(chick);
        master.feed(fish);*/

        //演示向上造型(多态)的第二点应用
        //Animal o = new Animal();//编译错误,抽象类不能被实例化
        Animal[] animals = new Animal[5];
        animals[0] = new Dog("小黑",2,"黑");//向上造型
        animals[1] = new Dog("小白",2,"白");
        animals[2] = new Fish("小金",2,"金");
        animals[3] = new Fish("小花",2,"花");
        animals[4] = new Chick("小灰",2,"灰");
        for (int i=0;i<animals.length;i++){
            /*System.out.println(animals[i].name);
            animals[i].drink();
            animals[i].eat();*/
            if (animals[i] instanceof Dog){
                Dog dog = (Dog) animals[i];
                dog.LookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick = (Chick) animals[i];
                chick.LayEggs();
            }
            if (animals[i] instanceof Swim){//适用于所有实现Swim接口的(会游泳的)
                Swim s = (Swim) animals[i];
                s.swim();
            }
        }

        //演示强转成功的条件
       /* Animal o = new Dog("小黑",2,"黑");
        Dog g = (Dog) o;
        Swim s = (Swim) o;
        //Fish f = (Fish) o;

        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof Fish);
        if (o instanceof Fish){
            Fish f = (Fish) o;
        }*/

    }
}
